package com.wrt.wm.controller;

import com.wrt.wm.config.StaticParameters;
import com.wrt.wm.entity.AjaxResult;
import com.wrt.wm.entity.FontParameters;
import com.wrt.wm.service.ParametersService;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.wrt.wm.util.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * 水印字体参数接口
 * 
 * @author 文瑞涛
 */
@RestController
@RequestMapping({ "/font/" })
public class FontParametersController {
	
	@Resource
	private ParametersService fontParametersService;

	/**
	 * 
	 * 获取当前设定的字体参数
	 * @param  req http请求
	 * 
	 * @return 封装后的结果
	 *
	 */
	@GetMapping({ "param" })
	public AjaxResult getCurrentFontParemeters(HttpServletRequest req) {
		HttpSession session = req.getSession();
		FontParameters fontParameters = (FontParameters)session.getAttribute(StaticParameters.FONTPARAM);
		if(null == fontParameters) {
			fontParameters = new FontParameters();
			fontParameters.initFontParameters();
			List<String> tempfonts = fontParametersService.getSystemFontList();
			// 若系统内没有字体名称为“宋体”
			if (!tempfonts.contains(fontParameters.getFontName())) {
				fontParameters.setFontName(tempfonts.get(0));
			}
			session.setAttribute(StaticParameters.FONTPARAM, fontParameters);
		}
		fontParameters.validateColor();
		fontParameters.validateEndColor();
		return AjaxResult.success("获取当前字体参数成功", fontParameters);
	}

	/**
	 * 
	 * 当前系统支持的字体名称（可能不全）
	 * 
	 * @return 封装后的结果
	 *
	 */
	@GetMapping({ "systemfont" })
	public AjaxResult getCurrentSystemFont() {
		List<String> fontsname = fontParametersService.getSystemFontList();
		if (fontsname.isEmpty()) {
			return AjaxResult.fail("获取当前系统字体失败");
		}
		return AjaxResult.success("获取当前系统字体成功", fontsname.stream().filter(WordUtils::isContainChinese).collect(Collectors.toList()));
	}
	
	/**
	 * 刷新系统字体名称列表
	 */
	@GetMapping({ "refresh" })
	public void refreshSystemFont() {
		fontParametersService.refreshFontList();
	}

	/**
	 * 
	 * 设定字体参数
	 * @param  req http请求
	 * @param  fp 字体参数对象
	 * 
	 * @return 封装后的结果
	 *
	 */
	@PostMapping({ "set" })
	public AjaxResult setFontParemeters(HttpServletRequest req, FontParameters fp) {
		if (!fp.validateColor()) {
			return AjaxResult.fail("第一个颜色设置异常");
		}
		if (!fp.validateEndColor()) {
			return AjaxResult.fail("第二个颜色设置异常");
		}
		HttpSession session = req.getSession();
		FontParameters currentFontParameters = (FontParameters)session.getAttribute(StaticParameters.FONTPARAM);
		currentFontParameters.setFontParameters(fp);
		session.setAttribute(StaticParameters.FONTPARAM, currentFontParameters);
		return AjaxResult.success("字体参数已更新", currentFontParameters);
	}
}
